
library(latentnet)
library(tidyverse)

load("data/latentnet_test_temp.RData")

## We recommend using 1:4000 below. 
msub = network::get.inducedSubgraph(M2, 1:4000)
msub
samp.fit3 <- ergmm(msub ~ euclidean(d=1),
                   verbose=T)
plot(samp.fit3, pie=T)

## save(samp.fit3, file="data/latentnet_test_results.RData)
## load("data/latentnet_test_results.RData")

est1 = samp.fit3$mcmc.pmode$Z
est2 = samp.fit3$mcmc.mle$Z

ltest = orgID.idx.map %>%
  filter(idx %in% 1:4000) %>%
  mutate(est1 = est1,
         est2 = est2,
         orgID = as.character(orgID))

load("data/acnet_scores_5_13_22.RData")

ltest = ltest %>% left_join(acnet_final, by="orgID") %>% select(-DIME, -hansford_score,
                                                                -crosson_score, -barbera_score)

cor(ltest$acnet_score, ltest$est1, use="complete.obs") #0.134
cor(ltest$acnet_score, ltest$est2, use="complete.obs") #0.130

`%notin%` <- function(x,y){!(x %in% y)}
preprocess_text <- function(x){
  x = str_replace_all(x,'\\s+',' ')
  x = tolower(x)
  return(x)
}


tmp = ltest%>% 
  select(orgname, est1, acnet_score) %>%
  mutate(ACLU  = grepl("civil liberties", orgname)) %>%
  pivot_longer(cols = c(est1, acnet_score),
               names_to = "score")

g = ggplot(tmp[tmp$score=="est1",]) +
  geom_density(aes(x = value,fill=ACLU), alpha=0.5) + 
  theme_bw() + xlab("Latent score")
g2 = ggplot(tmp[tmp$score!="est1",]) +
  geom_density(aes(x = value,fill=ACLU), alpha=0.5) +
  theme_bw()+ xlab("Latent score")
g
ggsave("figures/app_aclu_latentnet.pdf")
g2
ggsave("figures/app_aclu_ignet.pdf")
